package com.comtop.db.utils

import java.sql.Timestamp
import java.text.SimpleDateFormat
import scala.collection.mutable.ArrayBuffer
import java.util.Calendar

object dateUtils {

  val HOUR_0 = "00:00:00:000"
  val HOUR_1 = "1:00:00:000"
  val HOUR_2 = "2:00:00:000"
  val HOUR_3 = "3:00:00:000"
  val HOUR_4 = "4:00:00:000"
  val HOUR_5 = "5:00:00:000"
  val HOUR_6 = "6:00:00:000"
  val HOUR_7 = "7:00:00:000"
  val HOUR_8 = "8:00:00:000"
  val HOUR_9 = "9:00:00:000"
  val HOUR_10 = "10:00:00:000"
  val HOUR_11 = "11:00:00:000"
  val HOUR_12 = "12:00:00:000"
  val HOUR_13 = "13:00:00:000"
  val HOUR_14 = "14:00:00:000"
  val HOUR_15 = "15:00:00:000"
  val HOUR_16 = "16:00:00:000"
  val HOUR_17 = "17:00:00:000"
  val HOUR_18 = "18:00:00:000"
  val HOUR_19 = "19:00:00:000"
  val HOUR_20 = "20:00:00:000"
  val HOUR_21 = "21:00:00:000"
  val HOUR_22 = "22:00:00:000"
  val HOUR_23 = "23:00:00:000"
  
  
  
  
  /**
   * 将时间字符串转换为Timestamp格式
   */
   def getTimestamp(x:String) :java.sql.Timestamp = {
        val format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        
        try {  
            if (null == x || x == "")
              return null
            else {
              val d = format.parse(x);
              val t = new Timestamp(d.getTime());
              return t
            }
        } catch {  
          case e: Exception => println("cdr parse timestamp wrong")
         } 
        return null
    }

  /**
   * 构建一天24小时的时间点
   */
  def getHoursArr(startDay: String, endDay: String):Array[java.sql.Timestamp] = {
    val hour_0 = getTimestamp(startDay + " " + HOUR_0)
    val hour_1 = getTimestamp(startDay + " " + HOUR_1)
    val hour_2 = getTimestamp(startDay + " " + HOUR_2)
    val hour_3 = getTimestamp(startDay + " " + HOUR_3)
    val hour_4 = getTimestamp(startDay + " " + HOUR_4)
    val hour_5 = getTimestamp(startDay + " " + HOUR_5)
    val hour_6 = getTimestamp(startDay + " " + HOUR_6)
    val hour_7 = getTimestamp(startDay + " " + HOUR_7)
    val hour_8 = getTimestamp(startDay + " " + HOUR_8)
    val hour_9 = getTimestamp(startDay + " " + HOUR_9)
    val hour_10 = getTimestamp(startDay + " " + HOUR_10)
    val hour_11 = getTimestamp(startDay + " " + HOUR_11)
    val hour_12 = getTimestamp(startDay + " " + HOUR_12)
    val hour_13 = getTimestamp(startDay + " " + HOUR_13)
    val hour_14 = getTimestamp(startDay + " " + HOUR_14)
    val hour_15 = getTimestamp(startDay + " " + HOUR_15)
    val hour_16 = getTimestamp(startDay + " " + HOUR_16)
    val hour_17 = getTimestamp(startDay + " " + HOUR_17)
    val hour_18 = getTimestamp(startDay + " " + HOUR_18)
    val hour_19 = getTimestamp(startDay + " " + HOUR_19)
    val hour_20 = getTimestamp(startDay + " " + HOUR_20)
    val hour_21 = getTimestamp(startDay + " " + HOUR_21)
    val hour_22 = getTimestamp(startDay + " " + HOUR_22)
    val hour_23 = getTimestamp(startDay + " " + HOUR_23)
    val hour_24 = getTimestamp(endDay + " " + HOUR_0)
    
    val hoursArr = Array(hour_0,hour_1,hour_2,hour_3,hour_4,hour_5,hour_6,hour_7,hour_8,hour_9,hour_10,
        hour_11,hour_12,hour_13,hour_14,hour_15,hour_16,hour_17,hour_18,hour_19,hour_20,hour_21,hour_22,
        hour_23,hour_24)
        
    return hoursArr
  }
  

  /**
   * 获取时间中的周数
   */
  def getWeek(ts: Timestamp): Int = {
    val sdf = new SimpleDateFormat("yyyy-MM-dd")
    val date = sdf.parse(ts.toString())
    val calendar = Calendar.getInstance()
    calendar.setTime(date)
    
    return calendar.get(Calendar.WEEK_OF_YEAR)
  }
  
  
  
  
}